<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/soft/ShowIndex.class.php
 *
 * 显示软件下载的首页
 *
 * PHP Version 5
 *
 * @package:   class.article
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

//包含必要的软件工具类
include_once CLASS_PATH.'soft/SoftUtil.class.php';


//定义处理的类

class ShowIndex extends BaseAction {

   /**
    * 数据库的连接
    */
   private $db;


   /**
    * 每页显示软件数目
    */
   private $pre_page = 10;

   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 显示软件下载的首页
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {
      $smarty = $this->getSmarty();

      //取得所有的软件分类的格式
      $category = SoftUtil::getCategory($this->db);
      $smarty->assign('all_category', $category);


      //SEO 的内容
      $smarty->assign('clone_title', '软件下载 -- 5anet.com' );
      $smarty->assign('header_meta_content', '软件下载');

      //取得我们指定的软件分类
      $id = $this->getParameterFromGET('id');

      if ( $id ) {
         $smarty->assign('category', $id);
      }


       
      //取得本类下的软件
      //注意在这里我们需要取得一些变量来进行分页

      //取得页面
      $page = $this->getParameterFromGET('page');

      if ( !$page || $page <= 0 ) {
         $page = 1;
      }

      //求总的页面
      $total_page = SoftUtil::getAllPage($this->db, $this->pre_page, $id);

      if ( $page > $total_page && $total_page > 0 ) {
         $page = $total_page;
      }

      //页面
      $smarty->assign('now_page', $page);
      $smarty->assign('total_page', $total_page);

      $begin_page = 1;
      $end_page = $total_page;

      if ( $page <= 10 && $total_page >=10  ) {
         $end_page = 10; 
      } else if ( $page > 10  ) {
         if ( $page % 10 == 0 ) {
            //向前翻
            $end_page = $page; 
            $begin_page = $end_page - 9;
         } else if ( $page % 10 == 1 ) {
            //向后翻
            //确定开始的页数
            $begin_page = $page; 
            if ( $begin_page > $total_page ) {
               $begin_page = $page - 9;
            }
            if ( ( $begin_page + 9 ) > $total_page ) {
               $end_page = $total_page;
            } else {
               $end_page = $begin_page + 9;
            } 
   
         } else {
            $num = $page % 10;
            $pre_num = floor($page / 10 );
            $begin_page = $pre_num * 10 + 1;
            $end_page = $begin_page + 9;
         }
      }

      if ( $end_page > $total_page ) {
         $end_page = $total_page;
      }

      $nav_page_array = array();
      for( $i = $begin_page; $i<=$end_page; $i++ ) {
         array_push($nav_page_array, $i);
      }

      //帖子导航栏
      $smarty->assign('nav_page', $nav_page_array);

      $offset_page = ( $page - 1 ) * $this->pre_page;

      $soft_list = SoftUtil::getSoftList($this->db, $this->pre_page, $offset_page, $id);

      $smarty->assign('soft', $soft_list);



      $smarty->display('softindex.tmpl');

   }


}


?>
